<%--
    Document   : Table
    Created on : 24-nov-2010, 14:00:43
    Author     : Gebruiker
--%>

<%@page import="com.sampullara.poker.Card"%>
<%@page import="model.Player"%>
<%@page import="model.GameManager"%>
<%@page import="model.Game"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Poker Table</title>
        <link href="poker.css" rel="stylesheet" type="text/css">
        <script type="text/javascript" language="javascript">
            var xmlhttp;
            function getXMLHttpRequest() {
                if(window.XMLHttpRequest){
                    xmlhttp = new XMLHttpRequest();
                } else {
				xmlhttp = new ActiveObject("Microsoft.XMLHTTP");
				}
				return xmlhttp;

            }

            function autoTurn() {
                turn();
                setInterval( "turn", 1000);
            }

            function turn() {
                xmlhttp = getXMLHttpRequest();
                xmlhttp.onreadystatechange=getResponse;
                xmlhttp.open("get", "TableController.do?turn=get", true);
                xmlhttp.send(null);
                updateBoard();
            }


            function buttonsEnabled() {
                document.getElementById('check').disabled=false;
                document.getElementById('fold').disabled=false;
                document.getElementById('bet').disabled=false;
                document.getElementById('call').disabled=false;
                document.getElementById('raise').disabled=false;
            }

            function buttonsDisabled() {
                document.getElementById('check').disabled=true;
                document.getElementById('fold').disabled=true;
                document.getElementById('bet').disabled=true;
                document.getElementById('call').disabled=true;
                document.getElementById('raise').disabled=true;
            }

            function submitFunction(i) {
                var action = "";
                if (i == 1) {
                        action = "check";
                }
                if (i == 2) {
                        action = "fold";
                }
                if (i == 3) {
                        action = "call";
                }
                if (i == 4) {
                        action = "bet";
                }
                if (i == 5) {
                        action = "raise";
                }
                buttonsDisabled();
                xmlhttp = getXMLHttpRequest();
                xmlhttp.onreadystatechange=getResponse;
                var url = "TableController.do?turn=get&action=" + action;
                xmlhttp.open("GET", url, true);
                xmlhttp.send(null);

            }
          function updateBoard(){
                  <%
                    Game game = (Game) ((GameManager)getServletContext().getAttribute("gameManager")).getGame((String) session.getAttribute("game"));
                    if(game.isStarted()){
                        for(Player player: game.getPlayers()){
                            if(game.getActivePlayers().contains(player)){
                                for(Card card: player.getCards()){
                                    %>
                                        var responseDiv = document.getElementById(<%="player" + game.getPlayers().indexOf(player)%>);
                                        if(responseDiv.hasChildNodes()){
                                            responseDiv.removeChild(responseDiv.childNodes[0]);
                                        }
                                        var responseText = <%=card.getSuit() +  ", " + card.getRank()%>;
                                    <%
                                }
                            }
                        }
                    }
                  %>
          }

          function getResponse(){
                if ( xmlhttp.readyState==4 && xmlhttp.status==200){
                    if(xmlhttp.responseText=="PREFLOP"){
                        updateBoard();
                    }
                    if(xmlhttp.responseText=="true\r\n"){
                        buttonsEnabled();
                    } else {
                        buttonsDisabled();
                    }
                }
            }

        </script>
    </head>
    <body onLoad="autoTurn()">
                <div class="wrapper">
			<div class="left1">
			</div>
			<div class="left2">
                            <div id="player3"></div>
			</div>
			<div class="left3">
			</div>
		</div>
		<div class="wrapper">
			 <div class="left1">
                             <div id="player2"></div>
			 </div>
			 <div class="left2">
                             <div id="board"></div>
			 </div>
			 <div class="left3">
                             <div id="player4"></div>
			 </div>
		</div>
		<div class="wrapper">
			<div class="left1">
			</div>
			<div class="left2">
                            <div id="player1"></div>
			</div>
			<div class="left3">
                            <form action="#" >
                                <textarea cols="3" rows="3" name="betamount"></textarea>
                                <input type="button" id="check" name="check" value="Check" onClick="submitFunction(1)"/>
                                <input type="button" id="fold" name="fold" value="Fold" onClick="submitFunction(2)"/>
                                <input type="button" id="call" name="call" value="Call" onClick="submitFunction(3)"/>
                                <input type="button" id="bet" name="bet" value="Bet" onClick="submitFunction(4)"/>
                                <input type="button" id="raise" name="raise" value="Raise" onClick="submitFunction(5)"/>
                            </form>
			</div>
		 </div>
    </body>
</html>
